package com.sinoglobal.ningxia.activity.news;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.base.BaseActivity;
import com.sinoglobal.ningxia.adapter.NewsFragmentStatePagerAdapter;
import com.sinoglobal.ningxia.beans.NewsSectionListVo;
import com.sinoglobal.ningxia.downdata.imp.RemoteImpl;
import com.sinoglobal.ningxia.http.Code;
import com.sinoglobal.ningxia.utils.EquipmentUtil;
import com.sinoglobal.ningxia.utils.ItktLog;
import com.viewpagerindicator.TabPageIndicator;

/**
 * 
 * @author zhourihu
 * @createdate 2014年7月9日 下午1:48:01
 * @Description: TODO(用一句话描述该类做什么) 新闻 首页
 */
public class NewsHomeActivity extends BaseActivity {
	private ViewPager viewPager; // 标签页
	private TabPageIndicator indicator; // 指示器
	NewsFragmentStatePagerAdapter newsPagerAdapter;
	ProgressDialog progressDialog;
	private String tag;// 判断是哪个入口过来的

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_news_home);
		openChenJInShi();//开启沉浸式
		newsPagerAdapter = new NewsFragmentStatePagerAdapter(getSupportFragmentManager());		
		tag = getIntent().getStringExtra("TAG");
		initView();
		loadData();
	}

	@Override
	protected void onResume() {
		// Auto-generated method stub
		super.onResume();
		ItktLog.i("NewsHomeActivity.onResume");
	}

	private void initView() {
		// titleView.setText(getString(R.string.news));
		progressDialog = new ProgressDialog(this);
		progressDialog.setMessage(getString(R.string.loading_news));
		indicator = (TabPageIndicator) findViewById(R.id.viewPager_info);
		viewPager = (ViewPager) findViewById(R.id.vp1);
		viewPager.setOffscreenPageLimit(5);
		viewPager.setAdapter(newsPagerAdapter);
		indicator.setViewPager(viewPager);
//		if (tag.equals("read")) {
//			try {
//				indicator.setCurrentItem(8);
//				titleView.setText("阅读");
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//
//		} else {
//			titleView.setText(getString(R.string.news));
//		}
		titleView.setText(getString(R.string.news));
		indicator.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
//				if (position == 8) {
//					titleView.setText("阅读");
//				} else {
//					titleView.setText(getString(R.string.news));
//				}

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// myTask Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// myTask Auto-generated method stub

			}
		});
		// indicator.setCurrentItem(8);

	}

	private void loadData() {
		try {
			setView(RemoteImpl.getInstance().getNewsSectionListVo(true));
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (!EquipmentUtil.checkNetState(this)) {
			showShortToastMessage(getString(R.string.no_network));
			return;
		}
		new ItktAsyncTaskWithDialog<Void, Void, NewsSectionListVo>(getString(R.string.loading_news), true, true) {

			@Override
			public NewsSectionListVo before(Void... params) throws Exception {
				return RemoteImpl.getInstance().getNewsSectionListVo(false);
			}

			@Override
			public void after(NewsSectionListVo result) {
				setView(result);
			}
			@Override
			public void exception() {
			}

		}.execute();
		
	}

	private void setView(NewsSectionListVo result) {
		if (result != null) {
			if (!(Code.CODE_SUCCESS == result.getCode())) {
				showShortToastMessage(result.getMessage());
				return;
			}
		}
		newsPagerAdapter.setData(result);
		indicator.notifyDataSetChanged();
	}

}
